unit Tools;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, DB, ComCtrls, StdCtrls, Buttons, Grids,
  ExtCtrls, DBTables, DBGrids;

type
  TFormTools = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Bevel4: TBevel;
    EditTool: TEdit;
    BtnAdd: TBitBtn;
    BtnDelete: TBitBtn;
    BtnDone: TBitBtn;
    BtnHelp: TBitBtn;
    EditFilename: TEdit;
    MainMenu1: TMainMenu;
    MenuHelp: TMenuItem;
    BtnBrowse: TBitBtn;
    DialogOpen: TOpenDialog;
    LabelNon: TLabel;
    BtnUpdate: TBitBtn;
    Panel1: TPanel;
    Status: TLabel;
    DSTools: TDataSource;
    GridTools: TDBGrid;
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnDoneClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
    procedure GridToolsKeyPress(Sender: TObject; var Key: Char);
    procedure GridToolsDblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure EditToolKeyPress(Sender: TObject; var Key: Char);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnBrowseClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GridToolsCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure CheckExisting;
  public
    { Public declarations }
  end;

var
  FormTools: TFormTools;

implementation
uses
  DBClient;

{$R *.DFM}

procedure TFormTools.FormCreate(Sender: TObject);
begin
  CheckExisting;
end;

procedure TFormTools.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormTools.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('external_applications');
end;

procedure TFormTools.BtnDoneClick(Sender: TObject);
begin
  close;
end;

procedure TFormTools.BtnDeleteClick(Sender: TObject);
begin
  if not Datawell.TableTools.EOF then Datawell.TableTools.delete;
  CheckExisting;
end;

procedure TFormTools.GridToolsKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridToolsDblClick(sender);
  end;
end;

procedure TFormTools.GridToolsDblClick(Sender: TObject);
begin
  if Datawell.TableTools.EOF then exit;
  EditTool.text:=Datawell.TableTools.fieldbyname('Tool').asstring;
  EditFilename.text:=Datawell.TableTools.fieldbyname('Filename').asstring;
end;

procedure TFormTools.FormShow(Sender: TObject);
begin
  EditTool.text:='';
  EditFilename.text:='';
end;

procedure TFormTools.EditToolKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnBrowse.click;
  end;
end;

procedure TFormTools.BtnAddClick(Sender: TObject);
var
  i: integer;
  didfind, replacing: boolean;
  table: TClientDataset;
begin
  didfind:=false;
  replacing:=false;
  if TBitBtn(sender).tag = 1 then replacing:=true;
  if (replacing) and (Datawell.TableTools.fieldbyname('Tool').asstring = '') then begin
    messagedlg('There is no application link to replace',mtinformation,[mbok],0);
    EditTool.setfocus;
    exit;
  end;
  if (pos('"',EditTool.text) > 0) or (pos('&',EditTool.text) > 0) then begin
    messagedlg('Application links cannot contain quotes or ampersands',mtinformation,[mbok],0);
    EditTool.setfocus;
    exit;
  end;
  if not replacing then begin
    table:=TClientDataset.create(self);
    with table do begin
      CloneCursor(Datawell.TableTools, false);
      filtered := true;
      active:=true;
      first;
      i:=0;
      while not EOF do begin
        inc(i);
        next;
      end;
    end;
    table.free;
    if i >= 10 then begin
      messagedlg('You can only have 10 application links defined at a time',
        mtinformation,[mbok],0);
      BtnDelete.setfocus;
      exit;
    end;
  end;
  if EditTool.text = '' then begin
    messagedlg('You must enter a name for the application link',mtinformation,[mbok],0);
    EditTool.setfocus;
    exit;
  end;
  if EditFilename.text = '' then begin
    messagedlg('You must browse to the filename of the application link',mtinformation,[mbok],0);
    EditFilename.setfocus;
    exit;
  end;
  EditTool.text:=Datawell.CutTrailingSpaces(EditTool.text);
  with Datawell.TableTools do begin
    first;
    while not EOF do begin
      if fieldbyname('Tool').asstring = EditTool.text then begin
        didfind:=true;
        break;
      end;
      next;
    end;
  end;
  if didfind then replacing:=false;
  if not replacing then begin
    if didfind then begin
      if Datawell.TableGlobals.fieldbyname('ShowWarnings').asboolean then begin
        if messagedlg('Application link already exists.  Replace?',
          mtconfirmation,[mbyes,mbno],0) = mrno then begin
          EditTool.selectall;
          EditTool.setfocus;
          exit;
        end;
      end;
      Datawell.TableTools.edit;
    end
    else Datawell.TableTools.append;
    Datawell.TableTools.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableTools.fieldbyname('Tool').asstring:=EditTool.text;
    Datawell.TableTools.fieldbyname('Filename').asstring:=EditFilename.text;
    Datawell.TableTools.post;
  end
  else begin
    Datawell.TableTools.edit;
    Datawell.TableTools.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableTools.fieldbyname('Tool').asstring:=EditTool.text;
    Datawell.TableTools.fieldbyname('Filename').asstring:=EditFilename.text;
    Datawell.TableTools.post;
  end;
  EditTool.text:='';
  EditFilename.text:='';
  EditTool.setfocus;
  CheckExisting;
end;

procedure TFormTools.BtnBrowseClick(Sender: TObject);
var filename: string;
begin
  if fileexists(EditFilename.text) then DialogOpen.filename:=EditFilename.text
  else DialogOpen.initialdir:='C:\Program Files';
  if not DialogOpen.execute then exit;
  filename:=DialogOpen.filename;
  EditFilename.text:=filename;
  if EditTool.text = '' then begin
    while pos('\',filename) > 0 do delete(filename,1,pos('\',filename));
    delete(filename,length(filename)-3,4);
    filename:=Datawell.subs('&','',filename);
    EditTool.text:=filename;
  end;
  BtnAdd.setfocus;
end;

procedure TFormTools.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridTools) then begin
    BtnDelete.click;
    key:=0;
  end;
end;

procedure TFormTools.GridToolsCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  if not fileexists(Datawell.TableTools.fieldbyname('Filename').asstring) then begin
    abrush.color:=clred;
    afont.color:=clwhite;
  end;
end;

procedure TFormTools.CheckExisting;
var
  non: boolean;
  table: TClientDataset;
begin
  non:=false;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableTools, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      if not fileexists(fieldbyname('Filename').asstring) then begin
        non:=true;
        break;
      end;
      next;
    end;
  end;
  table.free;
  LabelNon.visible:=non;
end;

end.
